# -*- coding: utf-8 -*-
import time
from Common.handle_log import do_log
from Common.performance_page import PerformancePage
from Common.Main_page import MainPage
do_mainpage = MainPage(device="d_one")
do_performance = PerformancePage(device="d_one")
def performance(test_mode=False, play_mode=True,audio_mode=False, feng_yun_mode=False):

    def ensure_home_page():
        """确保当前页面是首页"""
        pagename = do_mainpage.get_page_name()
        time.sleep(2)
        if pagename == "离开爱奇艺页面":
            do_log.error(f"当前不在爱奇艺应用，请注意")
            raise
        if pagename != "首页":
            do_log.error(f"当前不在首页，在{pagename}，尝试返回首页")
            mainpage_flag = do_mainpage.enter_to_main_page()
            if not mainpage_flag:
                do_log.error(f"通过遥控器指令返回首页失败，尝试使用ADB指令")
                return False
            time.sleep(3)
        else:
            do_log.info(f"当前在首页，开始测试")
            return True

    def ensure_play_page():
        """确保当前页面是播放页"""
        pagename = do_mainpage.get_page_name()
        if pagename == "离开爱奇艺页面":
            do_log.error(f"当前不在爱奇艺应用，请注意")
            raise
        if pagename != "播放页":
            do_log.error(f"当前不在播放页，在{pagename}，跳过当前操作")
            return False
        do_log.info(f"当前在播放页，开始测试")
        return True



    do_log.info('==========开始执行用例Performance_NormalScene 性能长稳测试_常规播放与非播放混合场景==========')
    do_mainpage.close_IQIYI_app()
    time.sleep(2)
    do_mainpage.start_IQIYI_app()
    time.sleep(5)

    i = 1
    while True:  # 设置最大循环次数，避免无限循环
        do_log.error(f"***************整体测试第{i}轮***************")
        do_mainpage.save_img(f"第{i}轮测试前存图")

        try:
            if play_mode:
                do_mainpage.enter_to_main_page()
                do_mainpage.refresh()
                do_mainpage.click_search()
                time.sleep(2)
                do_mainpage.search_byText("仙台有树")
                do_mainpage.click_playNo1()
                time.sleep(8)
                do_mainpage.refresh()
            else:
                do_log.info(f"整体测试第{i}轮不测试播放模式")
        except Exception as e:
            do_log.error(f"00整体测试第{i}轮进入影片失败, 错误信息: {str(e)}，开始重试")
            ensure_home_page()

        if play_mode:
            if ensure_play_page():
                for step, func, params in [
                    ("01 退出、进入播放", do_performance.Play_001, {"number": 30}),
                    ("02 快进三次、快退三次", do_performance.Play_002, {"number": 10}),
                    ("03 分别选3、2、1集", do_performance.Play_003, {"number": 10}),
                    ("04 所有倍速切换一遍", do_performance.Play_004, {"number": 10}),
                    ("05 所有分辨率切换一遍", do_performance.Play_005, {"number": 10}),
                ]:
                    do_log.error(f"********整体测试第{i}轮 {step} 循环{params['number']}次***************")
                    do_mainpage.save_img(f"第{i}轮测试前存图_{step}")
                    func(flag=True, test_mode=test_mode, **params)
                    do_mainpage.refresh()

        if audio_mode and play_mode:
            if ensure_play_page():
                for step, func, params in [
                    ("06 视频模式切换音频模式", do_performance.Play_006, {"number": 10}),
                    ("07 音频模式下快进、快退", do_performance.Play_002, {"number": 10}),
                    ("08 音频模式下选集", do_performance.Play_003, {"number": 10}),
                    ("09 音频模式下切换倍速", do_performance.Play_004, {"number": 10}),
                ]:
                    do_log.error(f"********整体测试第{i}轮 {step} 循环{params['number']}次***************")
                    do_mainpage.save_img(f"第{i}轮测试前存图_{step}")
                    func(flag=True, test_mode=test_mode, **params)
                    do_mainpage.refresh()

        ensure_home_page()

        for step, func, params in [
            ("11 所有频道切换一次", do_performance.NOT_Play_001, {"number": 3}),
            ("12 进入个人中心", do_performance.NOT_Play_002, {"number": 10}),
            ("13 点击进入电视剧、电影、综艺频道", do_performance.NOT_Play_003, {"number": 3}),
            ("14 首页点击进入搜索", do_performance.NOT_Play_004, {"number": 10}),
        ]:
            do_log.error(f"********整体测试第{i}轮 {step} 循环{params['number']}次***************")
            do_mainpage.save_img(f"第{i}轮测试前存图_{step}")
            flag=ensure_home_page()
            func(flag=flag, test_mode=test_mode, **params)
            do_mainpage.refresh()

        if feng_yun_mode:
            for step, func, params in [
                ("15 筛选页上下滑动", do_performance.NOT_Play_005, {"number": 3}),
                ("16 风云榜上滑到底部", do_performance.NOT_Play_006, {"number": 3}),
            ]:
                do_log.error(f"********整体测试第{i}轮 {step} 循环{params['number']}次***************")
                do_mainpage.save_img(f"第{i}轮测试前存图_{step}")
                flag=ensure_home_page()
                func(flag=flag, test_mode=test_mode, **params)
                do_mainpage.refresh()

        i += 1
        if test_mode:
            do_mainpage.close_IQIYI_app()
            return None
        time.sleep(60)

    do_log.info("测试完成")